package fr.dauphine.spring.ctl;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import fr.dauphine.spring.bo.Adresse;
import fr.dauphine.spring.bo.Client;
import fr.dauphine.spring.dao.AdresseDAO;
import fr.dauphine.spring.dao.ClientDAO;

@Controller
@RequestMapping("adresse.html")
@SessionAttributes("client")
public class AdresseController {
	@Autowired
	ClientDAO clientDAO;
	@Autowired
	AdresseDAO adresseDAO;

	@RequestMapping(method = RequestMethod.GET, params = {})
	public String affichage(@ModelAttribute("client") Client client, ModelMap model) {
		try {
			System.out.println("id client to Adresse :" + client.getId());
			if (client.getAdresses().isEmpty()) {
				model.addAttribute("adresse", new Adresse());
			} else {
				model.addAttribute("adresse", client.getAdresses().get(0));
			}
			return "adresse";
		} catch (Exception ex) {
			ex.printStackTrace();
			return "erreur";
		}
	}

	@RequestMapping(method = RequestMethod.GET, params = { "new" })
	public String affichage(@ModelAttribute("client") Client client, ModelMap model,
			@RequestParam("new") String newAdresse) {
		try {
			System.out.println("New adress!");
			model.addAttribute("adresse", new Adresse());
			return "adresse";
		} catch (Exception ex) {
			ex.printStackTrace();
			return "erreur";
		}
	}

	@RequestMapping(method = RequestMethod.GET, params = { "idAdresse" })
	public String affichageAdresse(@ModelAttribute("client") Client client, ModelMap model,
			@RequestParam("idAdresse") String id) {
		try {
			System.out.println("id adresse :" + id);
			model.addAttribute("adresse", adresseDAO.getById(id));
			return "adresse";
		} catch (Exception ex) {
			ex.printStackTrace();
			return "erreur";
		}
	}

	@RequestMapping(params = "validation=Valider", method = RequestMethod.POST)
	public String validation(@ModelAttribute("adresse") Adresse adresse, BindingResult result,
			ModelMap model, @RequestParam("idClient") String id) throws Exception {
		System.out.println("id to find : " + id);
		Client client = clientDAO.get(id);
		adresse.setClient(client);
		System.out.println(client.getNom());
		if (client.getAdresses() != null) {
			client.getAdresses().add(adresse);
		} else {
			client.setAdresses(new ArrayList<Adresse>());
			client.getAdresses().add(adresse);
		}
		adresseDAO.save(adresse);
		clientDAO.save(client);
		return "redirect:clients.html";
	}

	@RequestMapping(params = "validation=Annuler", method = RequestMethod.POST)
	public String validation() {
		return "redirect:clients.html";
	}
}
